const express = require('express')
const router = express.Router()
const db = require('../../connection/Connection')
const jwt = require('jsonwebtoken')
const SECRET = 'asdfta219*&%^jWJSHFJ'


router.post('/getProjectInfo', async (req, res) => {
    const result = jwt.verify(req.body.token, SECRET)
    let sql = 'select departmentId from user_role where username = ?'
    db.base(sql, result.username, response_temp => {
        //验证用户对应角色的权限
        if(response_temp!=null&& response_temp.length > 0) {
            sql = 'SELECT projectId,projectName,projectLeader ,difficultLevel,departmentName,projectStart,projectEnd,projectContent,participants FROM project_view where departmentId = ?'
            let data = [response_temp[0].departmentId]
            if(req.body.projectName!=null){
                sql = 'SELECT projectId,projectName,projectLeader ,difficultLevel,departmentName,projectStart,projectEnd,projectContent,participants FROM project_view where departmentId = ? and projectName = ?'
                data = [response_temp[0].departmentId,req.body.projectName]
                console.log("指定项目名的查询")
            }
            //查找对应项目
            db.base(sql, data, response => {

                console.log(response)
                res.json(response)
            })
        }
    })
})

module.exports = router
